package com.personal.website.lottery.web.controller.admin;

import com.personal.website.lottery.web.model.Notice;
import com.personal.website.lottery.web.model.Page;
import com.personal.website.lottery.web.service.NoticeService;
import com.personal.website.lottery.web.service.PageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * Created by 10171225 on 2016/6/20.
 */
@Controller
@RequestMapping("/admin/editManage")
public class EditManageController {

    @Resource
    private NoticeService noticeService;
    @Resource
    private PageService pageService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/editNoticeManage")
    public String editNotice(Model model) {
        List<Notice> noticeList = this.noticeService.getNoticeList(10);
        model.addAttribute("noticeList", noticeList);
        return "admin/editManage/editNoticeManage";
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, Model model) {
        String pageType = request.getParameter("pageType");
        Page page = this.pageService.getPage(pageType);
        model.addAttribute("page", page);
        return "admin/editManage/pageEdit";
    }

    @RequestMapping("/noticeShow")
    public String noticeShow(HttpServletRequest request, Model model) {
        int noticeId = -1;
        getNotice(request, model, noticeId);
        return "notice";
    }

    @RequestMapping("/noticeEdit")
    public String noticeEdit(HttpServletRequest request, Model model) {
        int noticeId = -1;
        getNotice(request, model, noticeId);
        return "admin/editManage/noticeEdit";
    }

    @RequestMapping("/noticeCreate")
    @ResponseBody
    public String noticeCreate(HttpSession session, HttpServletRequest request) {
        String title = null;

        try {
            String content = URLDecoder.decode(request.getParameter("content"), "UTF-8");
            title = URLDecoder.decode(request.getParameter("title"), "UTF-8");

            Notice notice = new Notice();
            notice.setTitle(title);
            notice.setContent(content);
            notice.setSubmitTime(new Date());
            this.noticeService.insertNotice(notice);
        } catch (Exception e) {
            logger.error(String.format("创建主题为'%s'的公告失败！", title),e);
            return "公告创建失败！";
        }
        logger.info(String.format("创建主题为'%s'的公告成功！", title));
        return "公告创建成功！";
    }

    private void getNotice(HttpServletRequest request, Model model, int noticeId) {
        try {
            noticeId = Integer.parseInt(request.getParameter("noticeId"));
            Notice notice = this.noticeService.getNoticeById(noticeId);
            model.addAttribute("notice", notice);
        } catch (Exception e) {
            logger.error(String.format("加载公告'%d'时失败！", noticeId), e);
        }
    }

    @RequestMapping("/noticeEditSubmit")
    @ResponseBody
    public String noticeEditSubmit(HttpServletRequest request) {
        String title = null;
        try {
            String noticeId = URLDecoder.decode(request.getParameter("noticeId"), "UTF-8");
            title = URLDecoder.decode(request.getParameter("title"), "UTF-8");

            String content = URLDecoder.decode(request.getParameter("content"), "UTF-8");

            Notice notice = this.noticeService.getNoticeById(Integer.parseInt(noticeId));
            notice.setContent(content);
            notice.setTitle(title);
            if (this.noticeService.updateNotice(notice)) {
                logger.info(String.format("编辑主题为'%s'的公告，编辑成功！", title));
                return "编辑成功！";
            } else {
                logger.error(String.format("编辑主题为'%s'的公告，提交失败！", title));
                return "编辑失败！";
            }

        } catch (Exception e) {
            logger.error(String.format("编辑主题为'%s'的公告，提交失败！", title), e);
            return "编辑失败！";
        }
    }

    @RequestMapping("/noticeDelete")
    @ResponseBody
    public String noticeDelete(HttpServletRequest request) {
        int noticeId = -1;
        try {
            noticeId = Integer.parseInt(request.getParameter("noticeId"));
            Notice notice = this.noticeService.getNoticeById(noticeId);
            if (notice == null) {
                logger.error(String.format("删除公告'%d'时，公告不存在！", noticeId));
                return "不存在该文章！";
            }
            if (this.noticeService.deleteArticle(noticeId)) {
                logger.info(String.format("删除公告'%d'，删除成功！", noticeId));
                return "删除成功！";
            } else {
                logger.error(String.format("删除公告'%d'，删除失败！", noticeId));
                return "删除失败！";
            }
        } catch (Exception e) {
            logger.error(String.format("未知异常，删除失败！", noticeId), e);
            return "删除失败！";
        }
    }

    @RequestMapping("/pageEditSubmit")
    @ResponseBody
    public String pageEditSubmit(HttpServletRequest request) {
        String pageComment = "";
        try {
            String pageId = URLDecoder.decode(request.getParameter("pageId"), "UTF-8");
            String pageType = URLDecoder.decode(request.getParameter("pageType"), "UTF-8");
            String content = URLDecoder.decode(request.getParameter("content"), "UTF-8");

            Page page = this.pageService.getPage(pageType);
            page.setContent(content);
            pageComment = page.getComment();
            if (this.pageService.updatePage(page)) {
                logger.info(String.format("编辑'%s'页面，编辑成功！", pageComment));
                return "编辑成功！";
            } else {
                logger.error(String.format("编辑'%s'页面，编辑失败！", pageComment));
                return "编辑失败！";
            }

        } catch (Exception e) {
            logger.error(String.format("编辑'%s'页面，编辑失败！", pageComment), e);
            return "编辑失败！";
        }
    }

}
